<template>
  <div>
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card>
      <el-row :gutter="20" style="position: relative">
        <el-col :span="6">
          <el-input
            v-model="seachlist.loginName"
            placeholder="管理员名称"
            clearable
          >
            <el-button
              slot="append"
              @click="getsearchTableList"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-button
          style="position: absolute; right: 10px"
          class="btnprimary fontsize16"
          @click="showAdd"
          >增加</el-button
        >
      </el-row>
      <el-table :data="tableList" border stripe>
        <el-table-column label="管理员姓名" prop="realName"></el-table-column>
        <el-table-column
          label="账号"
          prop="loginName"
          width="300px"
        ></el-table-column>
        <el-table-column label="角色" prop="roleName"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="statusChanged(scope.row, scope.row.id)"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="showEdit(scope.row)"
              class="btnprimary btnsmall"
              size="mini"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!-- <el-pagination
			  @size-change="handleSizeChange"
			  @current-change="handleCurrentChange"
			  :current-page="pageCount"
			  :page-size="pageSize"
			  layout="total, sizes, prev, pager, next, jumper"
			  :total="total">
			</el-pagination> -->
    </el-card>

    <el-dialog title="添加平台" :visible.sync="addDialogVisible" width="50%">
      <el-form ref="addFormRef" :model="addForm" label-width="120px">
        <el-form-item label="管理员姓名">
          <el-input
            v-model="addForm.realName"
            placeholder="请设置管理员姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="chooseRole" placeholder="请选择角色">
            <el-option value="" label="请选择角色"></el-option>
            <el-option
              v-for="(item, index) in roleList"
              :value="item.id"
              :label="item.roleName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号">
          <el-input
            v-model="addForm.loginName"
            placeholder="请设置账号(用来登录后台)"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="addForm.password"
            show-password
            placeholder="请设置密码(用来登录后台)"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button class="btnprimary" @click="addSub">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="editDialogVisible" width="50%">
      <el-form ref="editFormRef" :model="editForm" label-width="120px">
        <el-form-item label="管理员姓名">
          <el-input
            v-model="editForm.realName"
            placeholder="请设置管理员姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="chooseRole" placeholder="请选择角色">
            <el-option value="" label="请选择角色"></el-option>
            <el-option
              v-for="(item, index) in roleList"
              :value="item.id"
              :label="item.roleName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="editForm.loginName" disabled=""></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="editForm.password"
            show-password
            placeholder="请设置密码(用来登录后台)"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button class="btnprimary" @click="editSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableList: [],
      pageCount: 1,
      pageSize: 20,
      total: 0,
      addForm: {},
      addDialogVisible: false,
      editForm: {},
      editDialogVisible: false,
      seachlist: { loginName: "" },
      disabled: false,
      roleList: [],
      chooseRole: "",
    };
  },
  created() {
    this.getTableList();
    this.getroleList();
  },
  methods: {
    async getroleList() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const { data: res } = await this.$http.get("/sys/role/list");
      setTimeout(() => {
        loading.close();
      }, 500);
      console.log(res);
      if (res.code == 702) {
        this.$router.push("/login");
        return this.$message.error("请登录");
      }
      this.roleList = res.data;
      // this.total = res.data.total
    },
    async getTableList() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const { data: res } = await this.$http.get(
        "/sys/admin/adminList?loginName=" + this.seachlist.loginName
      );
      console.log(res);
      this.tableList = res.data;
      // this.total = res.data.total
      setTimeout(() => {
        loading.close();
      }, 500);
    },
    getsearchTableList() {
      this.getTableList();
    },
    clear() {
      this.seachlist = { name: "", status: "" };
      this.getTableList();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getTableList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageCount = val;
      this.getTableList();
    },
    showAdd() {
      this.chooseRole = "";
      this.addForm = {
        realName: "",
        loginName: "",
        password: "",
        roleIds: [],
      };
      this.addDialogVisible = true;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    async addSub() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      if (
        this.chooseRole == "" ||
        this.chooseRole == undefined ||
        this.chooseRole == null
      ) {
        setTimeout(() => {
          loading.close();
        }, 500);
        return this.$message.error("请选择角色");
      } else {
        this.addForm.roleIds.push(this.chooseRole);
        const { data: res } = await this.$http.post(
          "/sys/admin/add",
          this.addForm
        );
        console.log(res);
        setTimeout(() => {
          loading.close();
        }, 500);
        if (res.code !== 200) {
          return this.$message.error(res.msg);
        } else {
          this.$message.success("修改成功");
          this.addDialogVisible = false;
          this.getTableList();
        }
      }
    },
    async showEdit(list) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.editForm = list;
      this.chooseRole = list.roleIds[0];
      this.editForm.roleIds = [];
      this.editDialogVisible = true;
      setTimeout(() => {
        loading.close();
      }, 500);
    },
    async editSub() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      if (
        this.chooseRole == "" ||
        this.chooseRole == undefined ||
        this.chooseRole == null
      ) {
        setTimeout(() => {
          loading.close();
        }, 500);
        return this.$message.error("请选择角色");
      } else {
        this.editForm.roleIds.push(this.chooseRole);
        const { data: res } = await this.$http.post(
          "/sys/admin/update",
          this.editForm
        );
        setTimeout(() => {
          loading.close();
        }, 500);
        if (res.code !== 200) {
          return this.$message.error(res.msg);
        } else {
          this.$message.success("修改成功");
          this.editDialogVisible = false;
          this.getTableList();
        }
      }
    },
    // 监听switch状态改变
    async statusChanged(tableList, id) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const { data: res } = await this.$http.post("/sys/admin/update", {
        id: id,
        status: tableList.status,
      });
      setTimeout(() => {
        loading.close();
      }, 500);

      if (res.code !== 200) {
        return this.$message.error("修改贷超借款营销状态失败");
      }
      this.$message.success("修改贷超借款营销状态成功");
      this.getTableList();
    },
  },
};
</script>

<style lang="less" scoped>
.scope-img {
  height: 50px;
  width: 50px;
}
.el-icon-success {
  font-size: 26px;
  color: green;
}
.el-icon-error {
  font-size: 26px;
  color: #d60404;
}
.formitem {
  display: flex;
  align-items: center;
}
.el-form-item {
  margin-bottom: 10px;
}
.inp-1 {
  width: 30%;
  margin-right: 10px;
}
.inp-2 {
  width: 20%;
}
.editIcon {
  width: 80px;
  height: 80px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
